﻿$(function () {
    var activeTab = 0, activePanel = null, activeTabCounter = 0;
    var tabCounter = 1; //主tab计数
    var tabTemplate = "<li id='#{navId}'><a href='#{href}'>#{label}</a> <span class='ui-icon ui-icon-close'>Remove Tab</span></li>";
    $.datepicker.setDefaults($.datepicker.regional["zh-CN"]);
    $("#sidebarTabs").tabs({ selected: 0 });
    mainTabs = $("#main").tabs({
        collapsible: false,
        activate: function (event, ui) {
            if (ui != null) {
                activeTab = ui.newTab;
                activePanel = ui.newPanel;
                activeTabCounter = ui.newPanel.attr("id").match(/\d+/i)[0];
                //alert((ui.newPanel.attr("id")+"a1a2a3").match(/\d+/i));
            }
        }
    });
    $("#main ul").on("dblclick", "li a", function (event) {
        removeTab(this, "a");
        event.stopPropagation();
    });
    $("#main ul").on("dblclick", "li", function (event) {

        event.stopPropagation();
    });
    $("#main ul").bind("dblclick", function (event) {
        addTab();
    });
    mainTabs.delegate("span.ui-icon-close", "click", function () {
        var panelId = $(this).closest("li").remove().attr("aria-controls");
        $("#" + panelId).remove();
        mainTabs.tabs("refresh");
    });
    //添加tab
    var addTab = function (tabTitle, tabContent) {
        var label = tabTitle || "新建选项卡",
        navId = "tabs-nav-" + tabCounter,
        id = "tabs-" + tabCounter,
        li = $(tabTemplate.replace(/#\{href\}/g, "#" + id).replace(/#\{label\}/g, label).replace(/#\{navId\}/g, navId)),
        tabContentHtml = tabContent || "Tab " + tabCounter + " content.";
        mainTabs.find("#mainTabsNav").append(li);
        mainTabs.append("<div id='" + id + "'><p>" + tabContentHtml + "</p></div>");
        mainTabs.tabs("refresh");
        tabCounter++;
    }
    addTab();

    mainTabs.tabs("option", "active", 0);

    window.GetPage = function (url, active, id) {
        var activeTabCounter = active;
        if (active == null)
            activeTabCounter = mainTabs.tabs("option", "active");
        if (id == null) {
            OpenPage(url);

            return;
        }

        $.get(url, { tc: activeTabCounter, id: id },
                function (data, textStatus) {
                    tcTemp = GetTabCounter(data);
                    $("#tabs-nav-" + tcTemp).find("a").text(GetTabPanelTitel(data));
                    $("#tabs-" + tcTemp).html(data);
                    jQuery.validator.unobtrusive.parse("#tabs-" + tcTemp);
                });
    }
    window.OpenPage = function (url) {
        var active = mainTabs.tabs("option", "active");
        url = "/admin"+url;
        //alert(activePanel.attr("id"));
        $.get(url, { tc: activeTabCounter, r: Math.random() },
                function (data, textStatus, jqXHR) {
                    tcTemp = jqXHR.getResponseHeader("TabCounter");
                    $("#tabs-nav-" + tcTemp).find("a").text(GetTabPanelTitel(data));
                    $("#tabs-" + tcTemp).html(data);
                    jQuery.validator.unobtrusive.parse("#tabs-" + tcTemp);
                });
        //activePanel.html();
    }
    //返回数据要更新哪一个PANEL
    function GetTabCounter(data) {
        var rePanel = new RegExp('<div id="tabCounter" style="display:none;">(.*?)</div>', "g"),
        p = null;
        if (rePanel.test(data)) {
            p = RegExp.$1;
        }
        return p;
    }
    //返回数据PANEL的标题
    function GetTabPanelTitel(data) {
        reTitle = new RegExp('<div id="pageTitle" style="display:none;">(.*?)</div>', "g"), title = "无标题页面";
        if (reTitle.test(data)) {
            title = RegExp.$1;
        }
        return title;
    }
    //
    window.SubmitBegin = function () {
        alert(1);
    }
    //
    window.SubmitSuccess = function (content) {
        alert(1);
    }



    Date.Format = function (format) {
        var o = {
            "M+": this.getMonth() + 1, //month
            "d+": this.getDate(),    //day
            "h+": this.getHours(),   //hour
            "m+": this.getMinutes(), //minute
            "s+": this.getSeconds(), //second
            "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
            "S": this.getMilliseconds() //millisecond
        }

        if (/(y+)/.test(format)) {
            format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        }

        for (var k in o) {
            if (new RegExp("(" + k + ")").test(format)) {
                format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
            }
        }
        return format;
    }


    $("#Button1").click(function () { SubmitBegin(); });
    $("#Button2").click(function () { mainTabs.tabs("option", "active", 0); });
    $("#Button3").click(function () { $("#c").attr('href', 'Admin/a/'); mainTabs.tabs("load", 1); mainTabs.tabs("refresh"); });
});
Date.prototype.Format = function (format) {
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(),    //day
        "h+": this.getHours(),   //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
        "S": this.getMilliseconds() //millisecond
    }

    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }

    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
}
